/**
 * 
 */
package com.aspire.android.heliar.raw.mms;

import java.util.List;

import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ImageSpan;

/**
 * @author panglei
 * 
 */
public class MMSRender {

	public static final String PARAGRAPH_SEPARATOR = "\n\n";
	public static final String SPAN_IMAGE = "img";

	public static SpannableStringBuilder render(MMS mms) {
		SpannableStringBuilder builder = new SpannableStringBuilder();

		String subject = mms.getSubject();

		builder.append(subject);
		builder.append(PARAGRAPH_SEPARATOR);

		List<MMSFrame> frames = mms.getFrames();
		for (MMSFrame frame : frames) {
			if (frame.getContentType().equals(MMSParser.CONTENT_TYPE_TEXT)) {

				MMSTextFrame textFrame = (MMSTextFrame) frame;
				builder.append(textFrame.getText());
			} else if (frame.getContentType()
					.equals(MMSParser.CONTENT_TYPE_JPG)
					|| frame.getContentType()
							.equals(MMSParser.CONTENT_TYPE_GIF)) {

				MMSImageFrame imgFrame = (MMSImageFrame) frame;
				ImageSpan span = new ImageSpan(imgFrame.getImage(),
						ImageSpan.ALIGN_BASELINE);
				SpannableStringBuilder imagebuilder = new SpannableStringBuilder(
						SPAN_IMAGE);

				imagebuilder.setSpan(span, 0, SPAN_IMAGE.length(),
						Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
				builder.append(imagebuilder);
			}
			builder.append(PARAGRAPH_SEPARATOR);
		}
		return builder;
	}
}
